<template>
  <div class="merchant-list">
    <div class="search-box">
      <span>关键字</span>
      <el-input v-model="keywords" placeholder="请输入商家名称"></el-input>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="primary" @click="create">创建</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column label="编号" prop="shop_id" width="100">
      </el-table-column>
      <el-table-column label="商家名称" prop="name"> </el-table-column>
      <el-table-column label="联系人" prop="person"> </el-table-column>
      <el-table-column label="电话" prop="phone"> </el-table-column>
      <el-table-column label="创建日期" prop="create_time"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChange"
      :current-page="page"
      :page-size="size"
      layout="total, prev, pager, next"
      :total="total"
      background
    ></el-pagination>

    <el-dialog
      :title="dialogType ? '创建商家' : '编辑商家'"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeModal"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="商家名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入商家名称"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="person">
          <el-input
            v-model="formData.person"
            placeholder="请输入联系人"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input
            v-model="formData.phone"
            placeholder="请输入电话"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/merchant";
export default {
  name: "MerchantList",
  data() {
    return {
      keywords: "",
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      dialogVisible: false,
      dialogType: true,
      formData: {
        name: "",
        person: "",
        phone: "",
      },
      shop_id: "",
      rules: {
        name: [
          {
            required: true,
            message: "请输入商家名称",
            trigger: "blur",
          },
        ],
        person: [
          {
            required: true,
            message: "请输入联系人",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入电话",
            trigger: "blur",
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确电话",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getListData();
  },
  methods: {
    getListData() {
      let data = {
        keywords: this.keywords,
        page: this.page,
        size: this.size,
      };
      api.getList(data).then((res) => {
        const { list, count } = res.data;
        this.tableData = list;
        this.total = count;
      });
    },
    search() {
      this.page = 1;
      this.getListData();
    },
    create() {
      this.dialogVisible = true;
      this.dialogType = true;
      this.shop_id = "";
      this.formData.name = "";
      this.formData.person = "";
      this.formData.phone = "";
    },
    edit(row) {
      this.dialogVisible = true;
      this.dialogType = false;
      const { shop_id, name, person, phone } = row;
      this.shop_id = shop_id;
      this.formData.name = name;
      this.formData.person = person;
      this.formData.phone = phone;
    },
    del(row) {
      this.shop_id = row.shop_id;
      this.$confirm("确定要永久删除此店铺吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api
            .delShop({ shop_id: this.shop_id })
            .then((res) => {
              if (res.code == 200200) {
                this.$message.success(res.msg);
                this.getListData();
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.shop_id = "";
        });
    },
    confirm() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          let data = { ...this.formData };
          let fn = this.dialogType
            ? api.createShop(data)
            : api.editShop({ ...data, shop_id: this.shop_id });
          fn.then((res) => {
            if (res.code == 200200) {
              this.Message.success(res.msg);
              this.dialogVisible = false;
              this.getListData();
            }
          }).catch((err) => {
            this.Message.error(res.msg);
          });
        } else {
          return false;
        }
      });
    },
    currentChange(val) {
      this.page = val;
      this.getListData();
    },
    closeModal() {
      this.$nextTick(() => {
        this.$refs["elForm"].clearValidate();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.merchant-list {
  padding: 40px;
  .search-box {
    span {
      font-size: 14px;
      color: #333;
      margin-right: 10px;
    }
    .el-input {
      width: 220px;
      margin-right: 15px;
    }
    margin-bottom: 20px;
  }
}
</style>
